/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

/* ========== Gio.Core.Editor ============================================ */

'Gio.Core.Editor'.namespace();

Gio.Core.Editor = Class.extend({
	init: function(name) {
		this._name 				   = name;
		
		this._jsPath    	  	   = '';
		this._jsFiles         	   = [];
		this._contentCss	  	   = null;
		
		this._theme  		  	   = null;
		this._availableThemes 	   = ['simple', 'default', 'advanced'];
		
		this._toolbars             = [];
		this._toolbarSeparator     = '';
		this._availabeToolbarItems = 
			[
			 	'CUT', 'COPY', 'PASTE', 'PASTE_TEXT', 'PASTE_FROM_WORD',
			 	'SEARCH', 'REPLACE',
			 	'LIST_BULLET', 'LIST_NUMBER',
			 	'OUTDENT', 'INDENT', 'QUOTE',
			 	'UNDO', 'REDO',
			 	'BOLD', 'ITALIC', 'STRIKE', 'STYLES', 'FORMAT', 'REMOVE_FORMAT',
			 	'LINK', 'UNLINK', 'ANCHOR', 'IMAGE',
			 	'TABLE',
			 	'PREVIEW'
			];
		
		this._plugins			   = [];
		this._availabePlugins 	   =
			[
			 	'FLASH'
			];
	},
	
	/**
	 * Getters
	 */
	getTheme: function() { return this._theme; },
	
	/**
	 * Setters
	 */
	setId: function(id) { 
		this._id = id;
		return this;
	},
	
	setWidth: function(width) { 
		this._width = width;
		return this;
	},
	
	setHeight: function(height) { 
		this._height = height;
		return this;
	},
	
	setSize: function(width, height) {
		this.setWidth(width);
		this.setHeight(height);
		return this;
	},
	
	setTheme: function(themeName) {
		this._theme = this._availableThemes[themeName];
		return this;
	},
	
	setJsPath: function(path) {
		this._jsPath = path;
		return this;
	},
	
	setContentCss: function(css) {
		this._contentCss = css;
		return this;
	},
	
	/**
	 * Add toolbar
	 * 
	 * @param array items
	 * @return Gio.Core.Editor
	 */
	addToolbar: function(items) {
		var toolbar = [];
		for (var i in items) {
			toolbar.push(this._availabeToolbarItems[items[i]]);
		}
		this._toolbars.push(toolbar);
		
		return this;
	},
	
	/**
	 * Add plugins
	 * 
	 * @param array plugins
	 * @return Gio.Core.Editor
	 */
	addPlugins: function(plugins) {
		for (var i in plugins) {
			this._plugins.push(this._availabePlugins[plugins[i]]);
		}
		return this;
	},
	
	loadJs: function() {
		/**
		 * Load the needed javascript files
		 */
		for (var i in this._jsFiles) {
			var file = this._jsPath + this._jsFiles[i];
			if ($('body').find('script[src="' + file + '"]').length == 0) {
				/**
				 * $('<script type="text/javascript" src="' + file + '"></script>').prependTo('body');
				 */
			}
		}
		return this;
	},

	/**
	 * Abstract methods
	 * TODO: Add more methods, such as adding plugins
	 */	
	
	/**
	 * Get the instance
	 */
	getInstance: function(id) {
	},
	
	/**
	 * Render the editor
	 * 
	 * @return Gio.Core.Editor
	 */
	show: function() {
	},	
	
	/**
	 * Insert HTML at the current position
	 * 
	 * @return void
	 */
	insertHtml: function(html) {
	}
});

/* ========== Gio.Core.Editor.TinyMCE ==================================== */

Gio.Core.Editor.TinyMCE = Gio.Core.Editor.extend({
	init: function(name) {
		this._super(name);
		this._jsFiles 			  = ['/tinymce/tiny_mce.js'];
		
		this._availableThemes 	  = 
			{
				'simple':   'simple',
				'default':  'advanced',
				'advanced': 'advanced'
			};
		this._toolbarSeparator     = '|';
		this._availabeToolbarItems = 
			{
			 	'CUT': 'cut', 'COPY': 'copy', 'PASTE': 'paste', 'PASTE_TEXT': 'pastetext', 'PASTE_FROM_WORD': 'pasteword',
			 	'SEARCH': 'search', 'REPLACE': 'replace',
			 	'LIST_BULLET': 'bullist', 'LIST_NUMBER': 'numlist',
			 	'OUTDENT': 'outdent', 'INDENT': 'indent', 'QUOTE': 'blockquote',
			 	'UNDO': 'undo', 'REDO': 'redo',
			 	'BOLD': '', 'ITALIC': '', 'STRIKE': '', 'STYLES': '', 'FORMAT': '', 'REMOVE_FORMAT': 'removeformat',
			 	'LINK': 'link', 'UNLINK': 'unlink', 'ANCHOR': 'anchor', 'IMAGE': 'image',
			 	'TABLE': 'tablecontrols',
			 	'PREVIEW': 'preview'
			};
		this._availabePlugins 	   =
			{
			 	'FLASH': 'media'
			};
	},
	
	getInstance: function(id) {
		return tinyMCE.getInstanceById(id);
	},
	
	show: function() {
		var settings = {
			theme: this._theme,
			width: this._width,
			height: this._height,
			mode: 'exact',
			elements: this._id,
			relative_urls: false,
			remove_script_host: false
		};
		/**
		 * TODO: Allows the parent class to set the toolbar location 
		 */
		settings.theme_advanced_toolbar_location = 'top';
		settings.theme_advanced_toolbar_align	 = 'left';
		
		var index = 0;
		for (var i in this._toolbars) {
			index = parseInt(i) + 1;
			if (this._toolbars[i].join(',').charAt(0) != ',') {
				settings['theme_advanced_buttons_' + index] = this._toolbars[i].join(',');
			}
		}
		
		if (this._contentCss != null) {
			settings.content_css = this._contentCss;
		}
		
		settings.plugins = this._plugins.join(',');
		
		/**
		 * TODO: Try to load the TinyMCE as following approach 
		 */
		/*
		if (typeof tinyMCE == 'undefined') {
			$.getScript(this._jsPath + this._jsFiles[0], function() {
				tinyMCE.init(settings);
			});
		}
		*/
		tinyMCE.init(settings);
	},
	
	insertHtml: function(html) {
		tinyMCE.getInstanceById(this._id).execCommand('mceInsertContent', false, html);
	}
});

/* ========== Gio.Core.Editor.CKEditor =================================== */

Gio.Core.Editor.CKEditor = Gio.Core.Editor.extend({
	init: function(name) {
		this._super(name);
		this._jsFiles 			   = ['/ckeditor/ckeditor.js'];
		
		this._availableThemes 	   = 
			{
				'simple':   'default',
				'default':  'default',
				'advanced': 'default'
			};
		this._toolbarSeparator     = '-';
		this._availabeToolbarItems = 
			{
			 	'CUT': 'Cut', 'COPY': 'Copy', 'PASTE': 'Paste', 'PASTE_TEXT': 'PasteText', 'PASTE_FROM_WORD': 'PasteFromWord',
			 	'SEARCH': 'Find', 'REPLACE': 'Replace',
			 	'LIST_BULLET': 'BulletedList', 'LIST_NUMBER': 'NumberedList',
			 	'OUTDENT': 'Outdent', 'INDENT': 'Indent', 'QUOTE': 'Blockquote',
			 	'UNDO': 'Undo', 'REDO': 'Redo',
			 	'BOLD': 'Bold', 'ITALIC': 'Italic', 'STRIKE': 'Strike', 'STYLES': 'Styles', 'FORMAT': 'Format', 'REMOVE_FORMAT': 'RemoveFormat',
			 	'LINK': 'Link', 'UNLINK': 'Unlink', 'ANCHOR': 'Anchor', 'IMAGE': 'Image',
			 	'TABLE': 'Table',
			 	'PREVIEW': 'Preview'
			};
		this._availabePlugins 	   =
			{
			 	'FLASH': 'flash'
			};
	},
	
	getInstance: function(id) {
		return CKEDITOR.instances[id];
	},
	
	show: function() {
		var settings = {
			toolbar: [],
			width: this._width + 'px',
			height: this._height + 'px'
		};
		for (var i in this._toolbars) {
			settings.toolbar.push(this._toolbars[i]);
		}
		settings.corePlugins = this._plugins.join(',');
		
		if (this._contentCss != null) {
			settings.contentsCss = this._contentCss;
		}
		CKEDITOR.replace(this._id, settings);
	},
	
	insertHtml: function(html) {
		CKEDITOR.instances[this._id].insertHtml(html);
	}
});

/* ========== Gio.Core.Editor.Factory ==================================== */

/**
 * Get the editor instance based on type
 */
'Gio.Core.Editor.Factory'.namespace();

Gio.Core.Editor.Factory = Class.extend({
	factory: function(editorType) {
		var editor;
		switch (editorType) {
			case 'tinymce':
				editor = new Gio.Core.Editor.TinyMCE(editorType);
				break;
			case 'ckeditor':
			default:
				editor = new Gio.Core.Editor.CKEditor(editorType);
				break;
		}
		return editor;
	}
});
